package cn.idietrade.idietradeusermodule8004.controller;

import cn.idietrade.common.pojo.SecondGoods;
import cn.idietrade.common.pojo.SecondUser;
import cn.idietrade.common.utils.EmptyUtils;
import cn.idietrade.common.utils.MD5;
import cn.idietrade.common.utils.ReturnResultData;
import cn.idietrade.idietradeusermodule8004.service.MsgSendService;
import cn.idietrade.idietradeusermodule8004.service.SecondUserService;
import cn.idietrade.idietradeusermodule8004.utils.EmailUtil;
import cn.idietrade.idietradeusermodule8004.utils.RedisUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SecondUserController {
    @Resource
    private SecondUserService userService;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private MsgSendService msgSendService;

    @Resource
    private EmailUtil emailUtil;

    //发送邮件
    @RequestMapping("/sendEmail")
    public String sendEmail(){
        boolean flag = emailUtil.sendEmailPlus();
        if (flag){
            return "发送成功";
        }else {
            return "发送失败";
        }
    }


    //发送验证码
    @RequestMapping("/sendCode")
    public String getCode(String phone){
        String code = "123456";
        //调用service方法，通过整合短信服务进行发送
        boolean isSend = msgSendService.send(phone,code);
        if(isSend){
            return "发送成功";
        }else{
            return "发送失败";
        }
    }

    //注册用户
    @RequestMapping("/register")
    public String register(String userName,String account,String password,String email,String phone,String identityCard){
        SecondUser user = new SecondUser();
        user.setUserName(userName);
        user.setAccount(account);
        user.setPassword(password);
        user.setEmail(email);
        user.setPhone(phone);
        user.setIdentityCard(identityCard);
        int flag = userService.register(user);
        if (flag == 1){
            return "注册成功";
        } else if (flag == 0) {
            return "注册失败";
        }else {
            return "账户已存在";
        }
    }


    //获取用户列表
    @RequestMapping("/getList")
    public List<SecondUser> getUserList(){
        List<SecondUser> list = new ArrayList<>();
        list = userService.getUserList();
        return list;
    }


    //登录方法
    @RequestMapping("/login")
    public ReturnResultData<SecondUser> login(String account, String password){
        ReturnResultData<SecondUser> resultData =new ReturnResultData<>();
        String token = "";
        try {
            SecondUser user = userService.login(account,password);
            if(EmptyUtils.isNotEmpty(user)){
                resultData.setCode(1);
                resultData.setMessage("用户登录成功");
                token = user.getAccount()+ MD5.getRandomCode();
                redisUtil.setStr(token, JSON.toJSONString(user),30L);
                user.setAccount(token);
                resultData.setData(user);
            }else{
                resultData.setCode(0);
                resultData.setMessage("用户登录失败");
                resultData.setData(null);
            }
        }catch (Exception e){
            resultData.setData(null);
            resultData.setCode(-1);
            resultData.setMessage("程序异常");
            e.printStackTrace();
        }
        return resultData;
    }

    //更新用户
    @RequestMapping("/update")
    public String updateInfo(SecondUser user){
        int flag = userService.updateUser(user);
        if (flag == 1){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }

    //逻辑删除用户
    @RequestMapping("delete")
    public String delUser(Integer id){
        Integer flag = userService.modifyUserPermission(id,-1);
        if (flag == 1){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    //逻辑恢复用户
    @RequestMapping("recover")
    public String recoverUser(Integer id){
        Integer flag = userService.modifyUserPermission(id,0);
        if (flag == 1){
            return "恢复成功";
        }else {
            return "恢复失败";
        }
    }
}
